package com.me.forum.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.forum.dao.ICategoryDao;
import com.me.forum.domain.Category;
import com.me.forum.request.CategoryRequest;
import com.me.forum.service.ICategoryService;
import org.springframework.stereotype.Service;

@Service
public class CategoryService  extends BaseService<ICategoryDao, Category> implements ICategoryService {
    @Override
    public IPage<Category> getCategory(CategoryRequest creq, String page, String size) {
        return dao.selectByAll(creq,new Page<Category>(Long.valueOf(size),Long.valueOf(size)));
    }

    @Override
    public Category addCategory(CategoryRequest creq) {
        if (!creq.isLegal()) {
            return null;
        }

        Category Category = dao.selectByName(creq.getName());
        if (Category != null) {
            return null;
        }

        int saveNum = dao.insertSelective(creq);
        return saveNum == 1? dao.selectByName(creq.getName()) : null;
    }

    @Override
    public boolean updateByName(CategoryRequest creq) {
        if (!creq.isLegal()) {
            return false;
        }
        return dao.updateByName(creq) == 1;
    }
}